//
//  EmoticonViewModel.swift
//  模拟键盘
//
//  Created by apple on 15/12/16.
//  Copyright © 2015年 idcast. All rights reserved.
//

import Foundation

class EmoticonManger {
    
    //创建单例
    static let sharedManger = EmoticonManger()
    
    //表情包模型
    lazy var packages = [EmoticonPackage]()
    
     private init() {
        //添加最近的分组
        packages.append(EmoticonPackage(dict: ["group_name_cn": "最近A"]))

        //1 加载emoticon.plist文件
        let path = NSBundle.mainBundle().pathForResource("emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle")!
        
        //2 加载字典
        let dict = NSDictionary(contentsOfFile: path) as! [String: AnyObject]
        
        //3 从字典中加载id 的数组 valueForKey 直接获取字典数组中的key对应的数组
        let array = (dict["packages"] as! NSArray).valueForKey("id")
//        let array = dict["packages"]!.valueForKey("id")
        
        //4 遍历 id 数组 准备加载info.plist
        for id in array as! [String] {
        loadInfoPlist(id)
        }
        print(packages)
    }
    //加载每一个id 下的info.plist文件
    private func loadInfoPlist(id: String) {
       //1 建立路径
        let path = NSBundle.mainBundle().pathForResource("info.plist", ofType: nil, inDirectory: "Emoticons.bundle/\(id)")!
        
//        print(path)
        //2 加载字典   -》 一个独立的表情包
        let dict = NSDictionary(contentsOfFile: path) as! [String: AnyObject]
        
//        print(dict)
        //3 字典转模型 追加到packages数组中
        
        packages.append(EmoticonPackage(dict: dict))
        
    }
}